﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConditionJudge.Question;
using System.Collections;
using System.Text.RegularExpressions;

namespace ConditionJudge.Answer
{
    class Jimson : IConditionJudge
    {
        public string Judgement(string expression, int x)
        {
            string exp = string.Empty;
            string[] arr = expression.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in arr)
            {
                exp += item;
            }

            if (exp.Contains("||") && exp.Contains("&&"))
            {
                if (exp.Contains("("))
                {

                }
                else
                    return "FF";
            }
            else
            {


                if (IsTrue(exp, x))
                    return "TT";
                else
                    return "TF";

            }
            return "FF";
        }

        bool IsTrue(string exp, int x)
        {
            string[] arr = exp.Split(new string[] { "X" }, StringSplitOptions.None);

            ArrayList ary = new ArrayList();

            foreach (var item in arr)
            {
                if (item != "")
                {
                    if (item.StartsWith("<=") || item.StartsWith(">=") || item.StartsWith("!="))
                    {
                        ary.Add(CheckExp(x, int.Parse(item.Substring(2, 1)), item.Substring(0, 2)));
                    }
                    else
                    {
                        ary.Add(CheckExp(x, int.Parse(item.Substring(1, 1)), item.Substring(0, 1)));
                    }
                    if (item.Length > 3)
                    {
                        switch (item.Substring((item.Length - 2), 2))
                        {
                            case "||": { ary.Add("||"); break; }
                            case "&&": ary.Add("&&"); break;
                            default: break;
                        }
                    }
                }
            }

            bool res = false;           
            if (ary.Count <= 2)
            {
                return (bool)ary[0];
            }
            else
            {
                if (ary.Count > 2)
                {  
                    if ((string)ary[1] == "||")
                    {
                        res = false;

                        foreach (var item in ary)
                        {
                            if (item.GetType() == false.GetType())
                            {
                                res = res || (bool)item;
                            }
                        }
                    }
                    if ((string)ary[1] == "&&")
                    {
                        res = true;
                        foreach (var item in ary)
                        {
                            if (item.GetType() == false.GetType())
                            {
                                res = res && (bool)item;
                            }
                        }
                    }
                }
            }
            
           

            return res;

        }

        bool CheckExp(int x, int y, string exp)
        {
            switch (exp)
            {
                case ">": return x > y;
                case ">=": return x >= y;
                case "<": return x < y;
                case "<=": return x <= y;
                case "=": return x == y;
                case "!=": return x != y;              
            }
            return true;
        }

        bool checkCon(bool b1, bool b2, string ex)
        {
            switch (ex)
            {
                case "||": return (b1 || b2);
                case "&&": return (b1 && b2);               
                default:
                    break;
            }
            return false;
        }
    }
}
